/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.manage;

import org.zhiqim.account.dbo.user.UserInfo;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.StdSwitchAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.paging.PageResult;
import org.zhiqim.kernel.util.Sqls;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 用户账号管理，包括列表、修改和删除
 *
 * @version v1.0.0 @author zouzhigang 2017-6-28 新建与整理
 */
public class UserAccountAction extends StdSwitchAction
{
    @Override
    protected void validateId(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("userId", "请选择一个用户"));
    }

    @Override
    protected void list(HttpRequest request) throws Exception
    {
        int pageNo = request.getParameterInt("page", 1);
        int userSex = request.getParameterInt("userSex", -1);//0表示保密，1表示男，2表示女
        String userKey = request.getParameter("userKey");
        
        Selector selector = new Selector();
        selector.addOrderbyDesc("userCreated");//用户注册时间
        if(userSex != -1)
            selector.addMaybe("userSex", userSex);
        
        if(Validates.isNotEmpty(userKey))
        {//条件(用户昵称或用户名)
            selector.addOr(new Selector().addMaybeLike("userName", userKey).addMaybeLike("userNick", userKey));
        }
        
        PageResult<UserInfo> result = Global.get(ZTable.class).page(UserInfo.class, pageNo, 10, selector);
        result.addConditionMap(request.getParameterMap());
        request.setAttribute("result", result);
    }

    @Override
    protected void modify(HttpRequest request) throws Exception
    {
        long userId = request.getParameterLong("userId");
        UserInfo user = Global.get(ZTable.class).item(UserInfo.class, userId);
        if(user == null)
        {
            request.returnHistory("该用户不存在，请重新选择");
            return;
        }
        
        request.setAttribute("user", user); 
    }

    @Override
    protected void update(HttpRequest request) throws Exception
    {
        long userId = request.getParameterLong("userId");
        int userStatus = request.getParameterInt("userStatus");
        int userSex = request.getParameterInt("userSex");
        
        Updater updater = new Updater();
        updater.addMust("userId", userId);
        updater.addField("userStatus", userStatus);
        updater.addField("userSex", userSex);
        updater.addField("userModified", Sqls.nowTimestamp());
               
        Global.get(ZTable.class).update(UserInfo.class, updater);
    }

    @Override
    protected void delete(HttpRequest request) throws Exception
    {
        long userId = request.getParameterLong("userId");
        Global.get(ZTable.class).delete(UserInfo.class,userId);
    }
    
    /****************************************************************************************/
    //只有列表，修改和删除，没有增加功能
    /****************************************************************************************/
    
    @Override
    protected void validateForm(HttpRequest request)
    {
    }
    
    @Override
    protected void add(HttpRequest request) throws Exception
    {
    }
    
    @Override
    protected void insert(HttpRequest request) throws Exception
    {
    }
}

